package com.codeTop;

import java.util.Arrays;

/**
 * @Author: shaki
 * @Date: 2021/8/18 14:43
 */
public class 快速排序 {
    public static void main(String[] args) {
        int[] nums = {5,2,3,1};
        quickSort(nums, 0, nums.length-1);
        System.out.println(Arrays.toString(nums));
    }
    public static void quickSort(int[] nums, int left, int right){
        if(left < right){
            int pivot = getPivot(nums, left, right);
            quickSort(nums, left, pivot-1);
            quickSort(nums, pivot+1, right);
        }
    }

    private static int getPivot(int[] nums, int left, int right) {
        int temp = nums[left];
        while (left < right){
            while (left < right && nums[right] > temp){
                right--;
            }
            nums[left] = nums[right];
            while(left < right && nums[left] <= temp){
                left++;
            }
            nums[right] = nums[left];
        }
        nums[left] = temp;
        return left;
    }

}
